<%@page import="com.wowangz.cms.common.utils.CodeCreater"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="com.wowangz.cms.common.utils.DataFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="com.wowangz.cms.dailyoperation.servicemanage.pojo.Charge"%>
<%@ page import="java.util.List"%>
<%-- 引入页面头 --%>
<%@ include file="/pageHead.jsp"%>
<form name="form1" id="form1" action="${systemctx}/sample/saveSample.web" method="post">
	<div class="pageMain">
		<div class="pageTitle"><%=MessageResourcesUtils.getMessage(request, "systemmanage.servicemanagement.title") %><span></span>
		<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.title1") %><span></span>
		<%=MessageResourcesUtils.getMessage(request, "servicemessage.services.ordermessage.title3") %></div>
		<div class="pageBody">
				<div class="service-wrap">
				<h3>选择服务(商户：${name})</h3>
				<div class="service-box">
				<%
				List<Charge> servicelist = (List<Charge>)request.getAttribute("servicelist");
				for(Charge charge:servicelist){
				%>
			  	<% if(charge.getIsmonth() == SysConstant.YesOrNo.YES && charge.getIsproposed() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr title="<%=charge.getServicedesc()==null?"":charge.getServicedesc()%>">
								<th rowspan="2" width="12%">
									<label>
										<input class="check" id="<%=charge.getServiceid() %>_checkbox" name="checkbox" type="checkbox" value="" /><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<label>
										<input class="radio" type="radio" id="<%=charge.getServiceid() %>_radio_pre" name="<%=charge.getServiceid() %>_radio" value="<%=charge.getProposedprice() %>" />
										按<b class="ico-vol">笔</b>计费：&nbsp;&nbsp;&nbsp;&nbsp;每笔<span class="price"><%=DataFormat.formatAmount(charge.getProposedprice(),2)%></span>元
									</label>
								</td>
								<td width="12%">
										购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_count" name="count" maxlength="8"/>笔
								</td>
								<td width="35%"></td>
							</tr>
							<tr title="<%=charge.getServicedesc()==null?"":charge.getServicedesc() %>">
								<td>
									<label>
										<input class="radio" type="radio" id="<%=charge.getServiceid() %>_radio_mon" name="<%=charge.getServiceid() %>_radio" value="<%=charge.getMonthprice() %>"/>
										按<b class="ico-vol">月</b>计费：&nbsp;&nbsp;&nbsp;&nbsp;每月<span class="price"><%=DataFormat.formatAmount(charge.getMonthprice(),2)%></span>元
									</label>
								</td>
								<td>
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_moncount" name="moncount" maxlength="4"/>月
								</td>
								<td>
									开始日期<input class="iss_datebox" type="text" id="<%=charge.getServiceid() %>_startdate" name="startdate" data-options="onSelect:changeSelectFun"/>
									结束日期：&nbsp;&nbsp;<span id="<%=charge.getServiceid() %>_enddate"></span>
								</td>
							</tr>
						</table>
					</div>
					<%}else if(charge.getIsmonth() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr title="<%=charge.getServicedesc()==null?"":charge.getServicedesc()%>">
								<th rowspan="2" width="12%">
									<label>
										<input class="check" name="checkbox" type="checkbox" id="<%=charge.getServiceid() %>_checkbox" name="checkbox"/><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<label>
										<input class="radio" type="radio" id="<%=charge.getServiceid() %>_radio_mon" name="<%=charge.getServiceid() %>_radio_mon" value="<%=charge.getMonthprice() %>" />
										按<b class="ico-vol">月</b>计费：&nbsp;&nbsp;&nbsp;&nbsp;每月<span class="price"><%=DataFormat.formatAmount(charge.getMonthprice(),2) %></span>元
									</label>
								<td width="12%">
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_moncount" name="moncount" maxlength="4">月</td>
								<td width="35%">
									开始日期<input type="text" class="iss_datebox" id="<%=charge.getServiceid() %>_startdate" name="startdate" data-options="onSelect:changeSelectFun"/>
									结束日期：&nbsp;&nbsp;<span id="<%=charge.getServiceid() %>_enddate"></span>
								</td>
							</tr>
							<tr>
								<td></td>
								<td></td>
								<td></td>
							</tr>
						</table>
					</div>
					<%}else if(charge.getIsproposed() == SysConstant.YesOrNo.YES){ %>
					<div class="service-list">
						<table class="service-table">
							<tr title="<%=charge.getServicedesc()==null?"":charge.getServicedesc()%>">
								<th rowspan="2" width="12%">
									<label>
										<input class="check" id="<%=charge.getServiceid() %>_checkbox" name="checkbox" type="checkbox" value=""/><%=charge.getServicename()%>
									</label>
								</th>
								<td width="24%">
									<input class="radio" type="radio" id="<%=charge.getServiceid() %>_radio_pre" name="<%=charge.getServiceid() %>_radio_pre" value="<%=charge.getProposedprice() %>">
									按<b class="ico-vol">笔</b>计费：&nbsp;&nbsp;&nbsp;&nbsp;每笔<span class="price"><%=DataFormat.formatAmount(charge.getProposedprice(),2) %></span>元
								</td>
								<td width="12%">
									购买<input class="txt-vol" type="text" id="<%=charge.getServiceid() %>_count" name="count" maxlength="8"/>笔</td>
								<td width="35%"></td>
							</tr>
							<tr>
								<td></td>
								<td></td>
								<td></td>
							</tr>
						</table>
					</div>
				</div>
					<%} %>
			<%}%>
				<div class="total-box heji">
					<h4>按笔合计：</h4>
					<ul>
						<li><span class="vol" id="count_num">0</span>笔</li>
						<li>共<span class="price" id="count_price">0.00</span>元
						</li>
					</ul>
				</div>
				<div class="total-box heji">
					<h4>按月合计：</h4>
					<ul>
						<li><span class="vol" id="month_num">0</span>月</li>
						<li>共<span class="price" id="month_price">0.00</span>元
						</li>
					</ul>
				</div>
				<div class="total-box zongji">
					<h4>总计：</h4>
					<ul>
						<li><span id="total_price">0.00</span>元</li>
					</ul>
				</div>
			</div>	
		</div>
		<div class="button-group">
			<input type="hidden" id="storeid" name="storeid" value="${storeid}" />
			<input type="button" class="btn btn-default" id="butSubmit" name="butSubmit" value="<%=MessageResourcesUtils.getMessage(request, "common.button.pay")%>" />
			<input type="button" class="btn btn-default" id="butBack" name="butBack" value="<%=MessageResourcesUtils.getMessage(request, "common.button.back")%>" />
		</div>	
	</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
	//loading页面
	/* var form = document.getElementById("form1");   
 	if (form.attachEvent){   
 		form.attachEvent("onload", function(){  
 	    document.getElementById("loading").style.display="none";  
 	    });   
 	} else {   
 		form.onload = function(){  
 	    document.getElementById("loading").style.display="none";  
 	    };   
 	}   */
	
	$('#butBack').click(function() {
		parent.document.location.reload();
		$.fn.closeIssWindow();
	});
	
	$("input:radio").click(function() {
		changeRadioType();
	});
	$('input:checkbox').click(function(){
		changeCheckboxInputType();
	});
	$("input:text").change(function() {
		if(validataInputNum(this.value)){
			changeInputMonthOrCount();
		}else{
			alert("输入值只能为正整数!");
		}
	});
	$('#butSubmit').click(function(){
		//var obj=document.getElementsByName("checkbox");
		var cknum=0,rdnum=0;
		$('input[name=checkbox]:checked').each(function(i){
			cknum=cknum+1;
		});
		$('input:radio:checked').each(function(i){
			rdnum=rdnum+1;
		});
		if(cknum>0){
			if(validateSubmitData()){
				var data="";
				$("input:checkbox:checked").each(function(i,val){
					var id=val.id.split("_")[0];
					//验证 收费方式
					var radiopre=false;
					if(document.getElementById(id+"_radio_pre")!=undefined){
						radiopre=document.getElementById(id+"_radio_pre").checked;	//按笔数
					}
					var radiomon=false;
					if(document.getElementById(id+"_radio_mon")!=undefined){
						radiomon=document.getElementById(id+"_radio_mon").checked;	//按月
					}
					if(radiopre && !radiomon){
						//按笔数
						var countval=$("#"+id+"_count").val();
						if(countval!=null && countval!='' && countval!='undefined' && 
								typeof(countval)!='undefined' && typeof(countval)!=undefined){
							if(countval > 0){
								if(validataInputNum(countval)){
									data=data+"<%=SysConstant.ChargeManage.TRADESUM%>:"+id+":"+countval+"##";
								}
							}
						}
					}else if(!radiopre && radiomon){
						var monthval=$("#"+id+"_moncount").val();
						if(monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
							if(monthval > 0){
								if(validataInputNum(monthval)){
									var startdate = $('#'+id+"_startdate").datebox('getValue');
									if(startdate!=null && startdate!=''){
										data=data+"<%=SysConstant.ChargeManage.MONTH%>:"+id+":"+monthval+":"+startdate+"##";
									}
								}
							}
						}
					}
				});
				if(data!=null && data!=''){
					if(cknum==rdnum){
						if(confirm("确定支付？")){
							submitData(data);
						}
					}else{
						if(confirm("存在未填写完整数据，是否提交已填写完整数据？")){
							submitData(data);
						}
					}
				}
			}
		}else{
			alert("请选择数据!");
		}
	});
});
function validateSubmitData(){
	var resFlag=false;
	var obj=document.getElementsByName("checkbox");
	for(var i=0;i<obj.length;i++){
		if(obj[i].checked){
			var id=obj[i].id.split("_")[0];
			//验证 收费方式
			var radiopre=false;
			if(document.getElementById(id+"_radio_pre")!=undefined){
				radiopre=document.getElementById(id+"_radio_pre").checked;	//按笔数
			}
			var radiomon=false;
			if(document.getElementById(id+"_radio_mon")!=undefined){
				radiomon=document.getElementById(id+"_radio_mon").checked;	//按月
			}
			if(radiopre && !radiomon){
				//按笔数
				var countval=$("#"+id+"_count").val();
				if(countval!=null && countval!='' && countval!='undefined' && 
						typeof(countval)!='undefined' && typeof(countval)!=undefined){
					if(countval == 0){
						alert("按笔计费,购买笔数不能为0!");
						resFlag=false;
						break;
					}else{
						if(validataInputNum(countval)){
							resFlag= true;
						}else{
							alert("按笔计费,购买笔数只能为正整数!");
							resFlag= false;
							break;
						}
					}
				}else{
					alert("按笔计费,购买笔数不能为空!");
					resFlag= false;
					break;
				}
			}else if(!radiopre && radiomon){
				//按月
				var monthval=$("#"+id+"_moncount").val();
				if(monthval!=null && monthval!='' && monthval!='undefined' && 
						typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
					if(monthval == 0){
						alert("按月计费,购买月数不能为0!");
						resFlag= false;
						break;
					}else{
						if(validataInputNum(monthval)){
							var startdate = $('#'+id+"_startdate").datebox('getValue');
							if(startdate!=null && startdate!=''){
								resFlag= true;
							}else{
								alert("按月计费,购买开始日期不能为空!");
								resFlag= false;
								break;
							}
						}else{
							alert("按月计费,购买月数只能为正整数!");
							resFlag= false;
							break;
						}
					}
					
				}else{
					alert("按月计费,购买月数不能为空!");
					resFlag= false;
					break;
				}
			}else{
				alert("请选择计费方式!");
				resFlag= false;
				break;
			}
		}
	}
	return resFlag;
}
function submitData(data){
	showSending();
	var storeid = $('#storeid').val();
	$.ajax({
		type : 'POST',
		url : '${systemctx}/dailyoperation/servicemanage/savesService.json?storeid='+storeid,
		data : {type:data,tranCode:'<%=CodeCreater.createAccountCode(CodeCreater.ServiceBuyCode.SERVICEBUY)%>'},
		dataType : 'json',
		success : function(data) {
			//显示AJAX请求后的提示信息
			closeSending();
			if(data.messagetype==1){
				alert(data.message);
				location.reload() 
			}else{
				alert(data.message);
			}
		}
	});
}
//日期变更  
function changeSelectFun(date){
	var dateid=this.id.split("_")[0];
	var len=(date.getMonth()+1);
	if(len<10){
		len='0'+len;
	}
	var day=date.getDate();
	if(date.getDate()<10){
		day='0'+day;
	}
	var dateval=date.getFullYear()+"/"+len+"/"+day;
	var curdate='<%=new SimpleDateFormat("yyyy/MM/dd").format(new Date())%>';
	if(dateval<curdate){
		alert("请选择当日之后的日期!");
		var newDate ='<%=new SimpleDateFormat("yyyy-MM-dd").format(new Date())%>';
		var objDate = new Date(newDate.replace(/-/,"/"));
		$("#"+this.id).datebox('setValue',newDate);
		var monthval=$("#"+dateid+"_moncount").val();
		if(validataInputNum(monthval)){
			addCurMonth(newDate,monthval,dateid);
		}
		return false;
	}else{
		var monthval=$("#"+dateid+"_moncount").val();
		if(validataInputNum(monthval)){
			dateval=date.getFullYear()+"-"+len+"-"+day;
			addCurMonth(dateval,monthval,dateid);
		}	
	}
}
function addCurMonth(sDate, num,dateid) {
	$.ajax({
		type: 'POST',
		url: '${systemctx}/dailyoperation/servicemanage/queryServicebuyForDate.json',
		data: {startdate:sDate,num:num},
		dataType: 'json',
		success: function (data) {
			if(data!=null && data!=''){
				$("#"+dateid+"_enddate").html(data.info);
			}
		}
	});
}
function changeDateSelectFunById(valid){
	var val=valid.split("_")[0]
	if(document.getElementById(val+"_startdate")!=undefined){
		var dateval=$("#"+val+"_startdate").datebox('getValue');
		var countval=$("#"+val+"_moncount").val();
		if(dateval!=null && dateval!='' && dateval!='undefined' && typeof(dateval)!='undefined' && typeof(dateval)!=undefined &&
			 countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined	){
			addCurMonth(dateval,countval,val);
		}
	}
}
//验证输入的 笔数或月数  是否为正整数
function validataInputNum(val){
	var tt=/^\d+$/g;
	if(tt.test(val)){
		return true;
	}else{
		return false;
	}
}
///单选框 变化
function changeRadioType(){
	var totalCountAmount=0.00,totalMonthAmount=0.00;
	var countNum=0,monthNum=0;
	$(":radio").each(function(i,val){
		totalCountAmount+=new Number($.reverseFormatAmount(calculationCountAmount(val.id)));
		totalMonthAmount+=new Number($.reverseFormatAmount(calculationMonthAmount(val.id)));
		countNum+=new Number(calculationCountNum(val.id));
		monthNum+=new Number(calculationMonthNum(val.id));
		changeDateSelectFunById(val.id);
	});
	var totailAmount=new Number($.reverseFormatAmount(totalCountAmount))+new Number($.reverseFormatAmount(totalMonthAmount));
	$("#count_num").html(countNum);
	$("#count_price").html($.formatAmount(totalCountAmount,2));
	$("#month_num").html(monthNum);
	$("#month_price").html($.formatAmount(totalMonthAmount,2));
	$("#total_price").html($.formatAmount(totailAmount,2));
}
///复选框 变化
function changeCheckboxInputType(){
	changeRadioType();
}
///输入框  笔数或月数
function changeInputMonthOrCount(){
	changeCheckboxInputType();
}
//计算 总笔数
function calculationCountNum(valid){
	var totalAmount=0,totalCount=0;
	//按笔数计算
	//获得单选按钮值
	var val=valid.split("_")[0]
	var radpreval=$("#"+val+"_radio_pre").val();
	var countval=$("#"+val+"_count").val();
	if(countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined){
		//如果复选框和单选同时都选中
		var checkedck=document.getElementById(val+"_checkbox").checked;
		var radiockeck=document.getElementById(val+"_radio_pre").checked;
		if(checkedck){
			if(radiockeck){
				if(valid==val+"_radio_pre"){
					if(validataInputNum(countval)){
						totalCount=new Number(countval);//笔数
					}else{
						totalCount=0;
					}
				}else{
					totalCount=0;
				}
			}else{
				//单选未选中
				totalCount=0;
			}
		}else{
			//复选框未选中
			totalCount=0;
		}
	}else{
		totalCount=0;
	}
	totalAmount=new Number(totalCount);//笔数总金额
	return totalAmount;
}
//计算 总月数
function calculationMonthNum(valid){
	var totalMonth=0;
	//按月数计算
	var val=valid.split("_")[0]
	var monthval=$("#"+val+"_moncount").val();
	if(monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
		var checkedck=document.getElementById(val+"_checkbox").checked;
		var radiockeck=document.getElementById(val+"_radio_mon").checked;
		if(checkedck){
			if(radiockeck){
				if(valid==val+"_radio_mon"){
					if(validataInputNum(monthval)){
						totalMonth=new Number(monthval);//月数
					}else{
						totalMonth=0;
					}
				}else{
					totalMonth=0;
				}
			}else{
				totalMonth=0;
			}
		}else{
			totalMonth=0;
		}
	}else{
		totalMonth=0;
	}
	return new Number(totalMonth);//总月数
}

//计算金额 按笔数  返回金额值
function calculationCountAmount(valid){
	var totalAmount=0.00,totalCount=0.00;
	//按笔数计算
	//获得单选按钮值
	var val=valid.split("_")[0]
	var radpreval=$("#"+val+"_radio_pre").val();
	var countval=$("#"+val+"_count").val();
	if(radpreval!=null && radpreval!='' && radpreval!='undefined' && typeof(radpreval)!='undefined' && typeof(radpreval)!=undefined &&
		countval!=null && countval!='' && countval!='undefined' && typeof(countval)!='undefined' && typeof(countval)!=undefined){
		//如果复选框和单选同时都选中
		var checkedck=document.getElementById(val+"_checkbox").checked;
		var radiockeck=document.getElementById(val+"_radio_pre").checked;
		if(checkedck){
			if(radiockeck){
				if(valid==val+"_radio_pre"){
					if(validataInputNum(countval)){
						var countAmount=new Number($.reverseFormatAmount(radpreval));//笔数金额
						var countNum=new Number($.reverseFormatAmount(countval));//笔数
						totalCount=countAmount.mul(countNum);
					}else{
						totalCount=0.00;
					}
				}else{
					totalCount=0.00;
				}
			}else{
				//单选未选中
				totalCount=0.00;
			}
		}else{
			//复选框未选中
			totalCount=0.00;
		}
	}else{
		totalCount=0.00;
	}
	totalAmount=new Number($.reverseFormatAmount(totalCount));//笔数总金额
	return totalAmount;
}
//计算金额 按月数  返回金额值
function calculationMonthAmount(valid){
	var totalAmount=0.00,totalMonth=0.00;
	//按月数计算
	var val=valid.split("_")[0]
	var radmonval=$("#"+val+"_radio_mon").val();
	var monthval=$("#"+val+"_moncount").val();
	if(radmonval!=null && radmonval!='' && radmonval!='undefined' && typeof(radmonval)!='undefined' && typeof(radmonval)!=undefined &&
	   monthval!=null && monthval!='' && monthval!='undefined' && typeof(monthval)!='undefined' && typeof(monthval)!=undefined){
		var checkedck=document.getElementById(val+"_checkbox").checked;
		var radiockeck=document.getElementById(val+"_radio_mon").checked;
		if(checkedck){
			if(radiockeck){
				if(valid==val+"_radio_mon"){
					if(validataInputNum(monthval)){
						var monthAmount=new Number($.reverseFormatAmount(radmonval));//月金额
						var monthNum=new Number($.reverseFormatAmount(monthval));//月数
						totalMonth=monthAmount.mul(monthNum);
					}else{
						totalMonth=0.00;
					}
				}else{
					totalMonth=0.00;
				}
			}else{
				totalMonth=0.00;
			}
		}else{
			totalMonth=0.00;
		}
	}else{
		totalMonth=0.00;
	}
	totalAmount=new Number($.reverseFormatAmount(totalMonth));//月数总金额
	return totalAmount;
}
</script>
<%-- 引入页面尾 --%>
<%@ include file="/pageFloor.jsp"%>